import 'package:flutter_test/flutter_test.dart';
import 'package:ghpfc_fmp/core/database_helper.dart';
import 'package:ghpfc_fmp/models/user.dart';
import 'package:ghpfc_fmp/services/user_service.dart';

void main() {
  group('认证功能测试', () {
    setUpAll(() async {
      // 初始化数据库
      await DatabaseHelper.initializeDatabaseFactory();
      final dbHelper = DatabaseHelper();
      await dbHelper.database;
    });

    group('用户验证测试', () {
      test('用户名验证 - 有效用户名', () {
        expect(User.validateUsername('admin'), isNull);
        expect(User.validateUsername('user123'), isNull);
        expect(User.validateUsername('test_user'), isNull);
      });

      test('用户名验证 - 无效用户名', () {
        expect(User.validateUsername(''), isNotNull);
        expect(User.validateUsername('ab'), isNotNull); // 太短
        expect(User.validateUsername('a' * 51), isNotNull); // 太长
        expect(User.validateUsername('user@name'), isNotNull); // 包含特殊字符
      });

      test('密码验证 - 有效密码', () {
        expect(User.validatePassword('password123'), isNull);
        expect(User.validatePassword('123456'), isNull);
        expect(User.validatePassword('abcdef'), isNull);
      });

      test('密码验证 - 无效密码', () {
        expect(User.validatePassword(''), isNotNull);
        expect(User.validatePassword('12345'), isNotNull); // 太短
        expect(User.validatePassword('a' * 21), isNotNull); // 太长
      });

      test('真实姓名验证 - 有效姓名', () {
        expect(User.validateRealName('张三'), isNull);
        expect(User.validateRealName('John Doe'), isNull);
        expect(User.validateRealName('李四'), isNull);
      });

      test('真实姓名验证 - 无效姓名', () {
        expect(User.validateRealName(''), isNotNull);
        expect(User.validateRealName('A'), isNotNull); // 太短
        expect(User.validateRealName('a' * 51), isNotNull); // 太长
      });

      test('邮箱验证 - 有效邮箱', () {
        expect(User.validateEmail('test@example.com'), isNull);
        expect(User.validateEmail('user@domain.org'), isNull);
        expect(User.validateEmail(''), isNull); // 可选字段
        expect(User.validateEmail(null), isNull); // 可选字段
      });

      test('邮箱验证 - 无效邮箱', () {
        expect(User.validateEmail('invalid-email'), isNotNull);
        expect(User.validateEmail('test@'), isNotNull);
        expect(User.validateEmail('@example.com'), isNotNull);
      });

      test('手机号验证 - 有效手机号', () {
        expect(User.validatePhone('13800138000'), isNull);
        expect(User.validatePhone('15912345678'), isNull);
        expect(User.validatePhone(''), isNull); // 可选字段
        expect(User.validatePhone(null), isNull); // 可选字段
      });

      test('手机号验证 - 无效手机号', () {
        expect(User.validatePhone('1234567890'), isNotNull); // 不是11位
        expect(User.validatePhone('1380013800a'), isNotNull); // 包含字母
        expect(User.validatePhone('1380013800'), isNotNull); // 不是11位
        expect(User.validatePhone('138001380001'), isNotNull); // 超过11位
      });
    });

    group('密码加密测试', () {
      test('密码加密和验证', () {
        final password = 'testpassword123';
        final hash = DatabaseHelper.hashPassword(password);
        
        expect(hash, isNotNull);
        expect(hash.length, greaterThan(0));
        expect(DatabaseHelper.verifyPassword(password, hash), isTrue);
        expect(DatabaseHelper.verifyPassword('wrongpassword', hash), isFalse);
      });

      test('相同密码产生相同哈希', () {
        final password = 'samepassword';
        final hash1 = DatabaseHelper.hashPassword(password);
        final hash2 = DatabaseHelper.hashPassword(password);
        
        expect(hash1, equals(hash2));
      });

      test('不同密码产生不同哈希', () {
        final password1 = 'password1';
        final password2 = 'password2';
        final hash1 = DatabaseHelper.hashPassword(password1);
        final hash2 = DatabaseHelper.hashPassword(password2);
        
        expect(hash1, isNot(equals(hash2)));
      });
    });

    group('用户模型测试', () {
      test('用户模型创建', () {
        final user = User(
          username: 'testuser',
          passwordHash: 'hash123',
          realName: '测试用户',
          role: 1,
          isActive: true,
          createdAt: DateTime.now(),
          updatedAt: DateTime.now(),
          isDeleted: false,
        );

        expect(user.username, equals('testuser'));
        expect(user.realName, equals('测试用户'));
        expect(user.role, equals(1));
        expect(user.isActive, isTrue);
        expect(user.isDeleted, isFalse);
      });

      test('用户角色判断', () {
        final adminUser = User(
          username: 'admin',
          passwordHash: 'hash',
          role: 2,
          isActive: true,
          createdAt: DateTime.now(),
          updatedAt: DateTime.now(),
          isDeleted: false,
        );

        final normalUser = User(
          username: 'user',
          passwordHash: 'hash',
          role: 1,
          isActive: true,
          createdAt: DateTime.now(),
          updatedAt: DateTime.now(),
          isDeleted: false,
        );

        expect(adminUser.isAdmin, isTrue);
        expect(adminUser.isUser, isFalse);
        expect(normalUser.isAdmin, isFalse);
        expect(normalUser.isUser, isTrue);
      });

      test('用户显示名称', () {
        final userWithRealName = User(
          username: 'testuser',
          passwordHash: 'hash',
          realName: '真实姓名',
          role: 1,
          isActive: true,
          createdAt: DateTime.now(),
          updatedAt: DateTime.now(),
          isDeleted: false,
        );

        final userWithoutRealName = User(
          username: 'testuser',
          passwordHash: 'hash',
          role: 1,
          isActive: true,
          createdAt: DateTime.now(),
          updatedAt: DateTime.now(),
          isDeleted: false,
        );

        expect(userWithRealName.displayName, equals('真实姓名'));
        expect(userWithoutRealName.displayName, equals('testuser'));
      });

      test('角色名称获取', () {
        final adminUser = User(
          username: 'admin',
          passwordHash: 'hash',
          role: 2,
          isActive: true,
          createdAt: DateTime.now(),
          updatedAt: DateTime.now(),
          isDeleted: false,
        );

        final normalUser = User(
          username: 'user',
          passwordHash: 'hash',
          role: 1,
          isActive: true,
          createdAt: DateTime.now(),
          updatedAt: DateTime.now(),
          isDeleted: false,
        );

        expect(adminUser.roleName, equals('管理员'));
        expect(normalUser.roleName, equals('普通用户'));
      });
    });

    group('用户服务测试', () {
      test('用户数据验证 - 有效数据', () {
        final validationResult = UserService.validateUserData(
          username: 'testuser',
          password: 'password123',
          realName: '测试用户',
          email: 'test@example.com',
          phone: '13800138000',
          department: '技术部',
          position: '工程师',
        );

        expect(validationResult['username'], isNull);
        expect(validationResult['password'], isNull);
        expect(validationResult['realName'], isNull);
        expect(validationResult['email'], isNull);
        expect(validationResult['phone'], isNull);
        expect(validationResult['department'], isNull);
        expect(validationResult['position'], isNull);
      });

      test('用户数据验证 - 无效数据', () {
        final validationResult = UserService.validateUserData(
          username: 'ab', // 太短
          password: '123', // 太短
          realName: '', // 为空
          email: 'invalid-email', // 无效邮箱
          phone: '1234567890', // 不是11位
        );

        expect(validationResult['username'], isNotNull);
        expect(validationResult['password'], isNotNull);
        expect(validationResult['realName'], isNotNull);
        expect(validationResult['email'], isNotNull);
        expect(validationResult['phone'], isNotNull); // 无效手机号应该返回错误
      });
    });
  });
} 